package course;

import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import course.method;
/**
 * Servlet implementation class Admin_add
 */
@WebServlet("/Admin_add")
public class Admin_add extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Admin_add() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String id = request.getParameter("idnum");
		String uname =request.getParameter("uname");
		String pw =request.getParameter("key");
		String age =request.getParameter("age");
		String school =request.getParameter("school");
		String sex =request.getParameter("sex");
		String phone =request.getParameter("phone");
		
		if(method.Connection("INSERT INTO studentinfo (idnum,uname,idkey,age,school,sex,phone) VALUES (\""+id+"\",\""+uname+"\",\""+pw+"\",\""+age+"\",\""+school+"\",\""+sex+"\",\""+phone+"\")")) {
			JOptionPane.showMessageDialog(null, "插入成功"); 
			response.sendRedirect("admin.jsp");
		}
		else {
			JOptionPane.showMessageDialog(null, "已存在该名学生或其他错误无法插入","错误",JOptionPane.ERROR_MESSAGE); 
			response.sendRedirect("admin.jsp");
		}
		
	}

}
